<template>
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>
                    <img src="../assets/images/logo.jpg" alt="">
                    <span>电商后台管理系统</span>
                </div>
                <el-button type="info" @click="loginOut">退出</el-button>
             </el-header>
            <!-- 页面主体区域 -->
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                 <div class="toggle-button" @click="toggleCollapse">
                        |||
                </div>
                <el-col>
                    <el-menu
                    default-active="1"
                    class="el-menu-vertical-demo"
                    unique-opened
                    background-color="#333744"
                    :default-active="activepath"
                    :collapse = 'isCollapse'
                    router
                    :collapse-transition="isTransition"
                    text-color="#fff"
                    active-text-color="#409EFF">
                    <!-- 这是一级菜单 -->
                    <el-submenu :index="item.id + ''" v-for="item in menuList" :key='item.id'>
                        <!-- 以及菜单的模板区域 -->
                        <template slot="title">
                            <i :class="iconsObj[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/' + cItem.path" v-for="cItem in item.children" :key='cItem.id' @click="saveNavState('/' + cItem.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{cItem.authName}}</span>
                        </template>
                        </el-menu-item>
                    </el-submenu>
                    </el-menu>
                </el-col>
            </el-aside>
            <!-- 右侧主体 -->
                <el-main>
                    <router-view />
                </el-main>
        </el-container>
    </el-container>

</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shang_pin_guan_li',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-chart'
      },
      isCollapse: false,
      isTransition: false,
      activepath: ''
    }
  },
  created () {
    this.getMenuList()
    this.activepath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有的菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status === 200) {
        this.menuList = res.data
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activepath = path
    }

  }
}
</script>

<style lang="less" scoped>
    .el-container{
        height: 100%;
        .el-header{
            background-color: #373d41;
            color: #fff;
             display: flex;
             justify-content: space-between;
             padding-left: 0;
             align-items: center;
             color: #fff;
             font-size: 20px;
             > div {
                 display: flex;
                 align-items: center;
                 span{
                     margin-left: 15px;
                 }
                 img{
                     width: 40px;
                     height: 40px;
                     border-radius: 50%;
                     margin-left: 10px;
                 }
             }
         }
        .el-aside{
            background-color: #333744;
            .el-menu{
                border-right: none;
            }
        }
        .el-main{
            background-color: #eafdf1;
        }
    }
    .iconfont {
        margin-right: 10px;
    }
    .toggle-button{
        background-color: #4A5064;
        color: #fff;
        font-size: 12px;
        line-height: 24px;
        text-align: center;
        letter-spacing: 0.2em;
        cursor: pointer;
    }

</style>
